// Copyright 2019 autocareai.com. All rights reserved.
// Use of this source code is governed by autocareai.com.

package mysql

import "github.com/jinzhu/gorm"

// 事务处理
func Tx(db *gorm.DB, txFunc func(*gorm.DB) error) (err error) {
	tx := db.Begin()

	defer func() {
		if p := recover(); p != nil {
			tx.Rollback()
			panic(p)
		} else if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()

	err = txFunc(tx)

	return err
}
